package com.hipac.qs.quartz;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.ListenerManager;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * Created by xiaogu on 2017/3/3.
 */
public class QuartzCrontab {

    // 简单起见 直接抛出异常
    public static void main(String[] args) throws Exception {
        // 创建一个Scheduler
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        //引进作业程序
        // 创建一个JobDetail，指明name，groupName，以及具体的Job类名，
        // 该Job负责定义需要执行任务
        JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
                .withIdentity("Quartz Job1", "Quartz Group1")
                .build();
        jobDetail.getJobDataMap().put("type", "Quartz Type1");

        // 创建一个每周触发的Trigger，指明星期几几点几分执行
        // 这里为了展示效果每5秒执行1次

        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("Quartz CronTrigger1", "Quartz CronTrigger group1")
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                .build();

        scheduler.scheduleJob(jobDetail, trigger);

        ListenerManager listenerManager = scheduler.getListenerManager();
        listenerManager.addJobListener(new MyJobListener());
        listenerManager.addTriggerListener(new MyTriggerListener());

        scheduler.start();
    }
}


